package com.jiumi.api.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.jiumi.api.base.BaseApiController;
import com.jiumi.baseconfig.domain.*;
import com.jiumi.baseconfig.service.*;
import com.jiumi.common.config.AliConfig;
import com.jiumi.common.config.JiumiConfig;
import com.jiumi.common.core.domain.AjaxResult;
import com.jiumi.common.core.domain.R;
import com.jiumi.common.core.domain.entity.SysDictData;
import com.jiumi.common.core.page.TableDataInfo;
import com.jiumi.common.core.redis.RedisCache;
import com.jiumi.common.utils.DictUtils;
import com.jiumi.common.utils.SMSCode;
import com.jiumi.common.utils.file.FileUploadUtils;
import com.jiumi.common.utils.file.FileUtils;
import com.jiumi.order.domain.BaseOrderInfo;
import com.jiumi.order.service.IBaseOrderInfoService;
import com.jiumi.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lun.zhang
 * @create 2022/11/3 9:56
 */
@RequestMapping("/api")
@RestController
public class CommonApiController extends BaseApiController {

    @Autowired
    private IBaseBannerInfoService baseBannerInfoService;

    @Autowired
    private IBaseTextContentService baseTextContentService;

    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private IBaseCourseInfoService baseCourseInfoService;

    @Autowired
    private IBaseTeacherService baseTeacherService;

    @Autowired
    private IBaseConfigService baseConfigService;

    @Autowired
    private IBaseMealService baseMealService;

    @Autowired
    private IBaseChapterService baseChapterService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private IBaseUserService userInfoService;



    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file) throws Exception {
        try {
            // 上传文件路径
            String filePath = JiumiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = AliConfig.getImgPrefix() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("name", file.getOriginalFilename());
            ajax.put("url", url);
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 获取验证码
     */
    @GetMapping("/noAuth/getPhoneCode")
    public AjaxResult getPhoneCode(@RequestParam(value = "phone", required = true) String phone) {
        AjaxResult result = AjaxResult.success();
//        try {
//            result = SMSCode.sendSms(redisCache, phone);
//        } catch (Exception e) {
//            e.printStackTrace();
//            result = AjaxResult.error(e.getMessage());
//        }
        return result;
    }

    /**
     * 查询轮播图列表
     */
    @GetMapping("/noAuth/getBannerList")
    public AjaxResult getBannerList(@RequestParam(value = "categoryId", required = true) Long categoryId) {
        BaseBannerInfo banner = new BaseBannerInfo();
        banner.setCategoryId(categoryId);
        List<BaseBannerInfo> list = baseBannerInfoService.selectBaseBannerInfoList(banner);
        return AjaxResult.success(list);
    }


    @GetMapping("/noAuth/getProtocolDetail")
    public AjaxResult getProtocolDetail(@RequestParam(value = "id", required = true) Long id) {
        BaseTextContent text = baseTextContentService.selectBaseTextContentById(id);
        return AjaxResult.success(text);
    }

    @GetMapping("/noAuth/queryDictLabels")
    public AjaxResult queryDictLabels(@RequestParam(value = "type", required = true) String type) {
        List<SysDictData> list = DictUtils.getDictCache(type);
        if (list == null || list.size() == 0) {
            return AjaxResult.error("未查询到数据");
        }
        return AjaxResult.success(list);
    }

    @GetMapping("/noAuth/queryLabelData")
    public AjaxResult queryLabelData() {
        SysDictData param=new SysDictData();
        param.setStatus("0");
        List<SysDictData> dictData= dictDataService.selectDictDataList(param);
        JSONArray array=new JSONArray();
        dictData.stream().forEach(data->{
            JSONObject obj=new JSONObject();
            obj.put("dictType",data.getDictType());
            obj.put("dictLabel",data.getDictLabel());
            obj.put("dictValue",data.getDictValue());
            array.add(obj);
        });
        return AjaxResult.success(array);
    }

    /**
     * 首页数据查询
     */
    @GetMapping("/noAuth/getHomePageData")
    public AjaxResult getHomePageData() {
        List<BaseCourseInfo> goodsCourxse= baseCourseInfoService.selectCourseInfoHomeData();
        AjaxResult dataResult= AjaxResult.success();
        dataResult.put("goodCourse",goodsCourxse);

        BaseCourseInfo courseParam=new BaseCourseInfo();
        courseParam.setCourseTypeId(4L);
        startPage(1,4,"c.create_time","desc");
        List<BaseCourseInfo> videoList = baseCourseInfoService.selectBaseCourseInfoList(courseParam);
        dataResult.put("videoCourse",getDataTable(videoList).getRows());

        BaseCourseInfo liveParam=new BaseCourseInfo();
        liveParam.setCourseTypeId(2L);
        startPage(1,4,"c.create_time","desc");
        List<BaseCourseInfo> liveList = baseCourseInfoService.selectBaseCourseInfoList(liveParam);
        dataResult.put("liveCourse",getDataTable(liveList).getRows());

        BaseCourseInfo openParam=new BaseCourseInfo();
        openParam.setCourseTypeId(1L);
        startPage(1,4,"c.create_time","desc");
        List<BaseCourseInfo> openList = baseCourseInfoService.selectBaseCourseInfoList(openParam);
        dataResult.put("openCourse",getDataTable(openList).getRows());

        BaseTeacher teacherParam=new BaseTeacher();
        teacherParam.setShowIndex("Y");
        List<BaseTeacher> teacherList= baseTeacherService.selectBaseTeacherList(teacherParam);
        dataResult.put("referrerTeacher",teacherList);
        return dataResult;
    }

    /**
     * 课程查询
     */
    @GetMapping("/noAuth/getCourseData")
    public TableDataInfo getCourseData(BaseCourseInfo baseCourseInfo) {
        baseCourseInfo.setShowFlag("Y");
        startOrderBy();
        startPage();
        List<BaseCourseInfo> list = baseCourseInfoService.selectBaseCourseInfoList(baseCourseInfo);
        return getDataTable(list);
    }

    /**
     * 授课老师查询
     */
    @GetMapping("/noAuth/getTeacherData")
    public TableDataInfo getTeacherData(BaseTeacher baseTeacher) {
        startOrderBy();
        startPage();
        List<BaseTeacher> list = baseTeacherService.selectBaseTeacherList(baseTeacher);
        return getDataTable(list);
    }

    /**
     * 网站设置数据
     */
    @GetMapping("/noAuth/getWebsitData")
    public R<BaseConfig> getWebsitData() {
        List<BaseConfig> list = baseConfigService.selectBaseConfigList(new BaseConfig());
        BaseConfig configInfo=new BaseConfig();
        if(list.size()>0){
            configInfo= list.get(0);
        }
        return R.ok(configInfo);
    }

    /**
     * 课程套餐
     */
    @GetMapping("/noAuth/getMealData")
    public R<List<BaseMeal>> getMealData(BaseMeal meal) {
        meal.setShowFlag("Y");
        List<BaseMeal> mealList= baseMealService.selectBaseMealList(meal);
        return R.ok(mealList);
    }




}
